Certified Java Programmer Mock Exam


Question 1


class A {
  public static void main (String args[]) {
    int i = 0, j = 0, k = 0;
label1:
    for (int h = 0; h < 6; h++) {
label2:
      do {
        i++;
        k = h + i + j;
        switch (k) {
          default: break label1;
          case 1: continue label2;
          case 2: break;
          case 3: break label2;
          case 4: continue label2;
          case 5: continue label1;
        }
      } while (++j<5);
    }
    System.out.println(h + "," + i + "," + j);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0,1,0
b. Prints: 0,2,1
c. Prints: 1,3,1
d. Prints: 2,4,1
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 2


class A {
  public static void main (String[] args) {
    for (int i = 0; i < 4; i++) {
      switch (i) {
        case 0: System.out.print("A");
        case 1: System.out.print("B");
        case 2: System.out.print("C");
      }
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: ABC
b. Prints: ABCC
c. Prints: CBA
d. Prints: ABCBCC
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 3


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
    int a,b,c,d,f,g,x;
    a = b = c = d = f = g = 0;
    x = 5;
    try {
      try {
	switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
          case 4: throw new Exception();
        }
        a++;
      }
      catch (Level2Exception e) {b++;}
      finally{c++;}
    }
    catch (Level1Exception e) { d++;}
    catch (Exception e) {f++;}
    finally {g++;}
    System.out.print(a+","+b+","+c+","+d+","+f+","+g);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 1,0,0,0,0,0
b. Prints: 1,0,1,0,0,1
c. Prints: 0,0,1,0,0,1
d. Prints: 1,1,1,1,1,1
e. Compiler Error
f. Run Time Error
g. None of the Above

Question 4


class RedException extends Exception {}
class BlueException extends Exception {}
class White {
  void m1() throws RedException {throw new RedException();}
  public static void main (String[] args) {
    White white = new White();
    int a,b,c,d;
    a = b = c = d = 0;
    try {white.m1();a++;}
    catch (RedException e) {b++;}
    catch (BlueException e) {c++;}
    finally {d++;}
    System.out.print(a+","+b+","+c+","+d);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 5


1. class A {
2.   void m1() {throw new ClassNotFoundException();}
3.   void m2() {throw new ArithmeticException();}
4.   void m3() {throw new ClassCastException();}
5.   void m4() {throw new IllegalArgumentException();}
6.   void m5() {throw new CloneNotSupportedException();}
7.   void m6() {throw new NoSuchFieldException();}
8. }

What is the result of attempting to compile the program?

a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above

Question 6


1. class A {
2.   void m1() {throw new IndexOutOfBoundsException();}
3.   void m2() {throw new InstantiationException();}
4.   void m3() {throw new IllegalAccessException();}
5.   void m4() {throw new NullPointerException();}
6.   void m5() {throw new NoSuchMethodException();}
7.   void m6() {throw new SecurityException();}
8. }

What is the result of attempting to compile the program?

a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above

Question 7


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
  public static void main(String args[]) {
    int a, b, c, d, f;
    a = b = c = d = f = 0;
    int x = 1;

    try {
      switch (x) {
        case 1: throw new Level1Exception();
        case 2: throw new Level2Exception();
        case 3: throw new Level3Exception();
      }
      a++;
    }
    catch (Level3Exception e) { b++;}
    catch (Level2Exception e) { c++;}
    catch (Level1Exception e) { d++;}
    finally {f++;}
    System.out.print(a+","+b+","+c+","+d+","+f);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,1,1
b. Prints: 0,0,1,1,1
c. Prints: 0,1,1,1,1
d. Prints: 1,1,1,1,1
e. Prints: 0,0,1,0,1
f. Prints: 0,1,0,0,1
g. Prints: 1,0,0,0,1
h. Compiler Error
i. Run Time Error
j. None of the Above

Question 8


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
    int a,b,c,d,f,g,x;
    a = b = c = d = f = g = 0;
    x = 1;
    try {
      try {
        switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
        }
        a++;
      }
      catch (Level2Exception e) {b++;}
      finally{c++;}
    }
    catch (Level1Exception e) { d++;}
    catch (Exception e) {f++;}
    finally {g++;}
    System.out.print(a+","+b+","+c+","+d+","+f+","+g);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,1,0,0
b. Prints: 0,0,1,1,0,1
c. Prints: 0,1,1,1,0,1
d. Prints: 1,0,1,1,0,1
e. Prints: 1,1,1,1,0,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 9


1. class A {
2.   void m1() {throw new ClassNotFoundException();}
3.   void m2() {throw new CloneNotSupportedException();}
4.   void m3() {throw new IllegalAccessException();}
5.   void m4() {throw new InstantiationException();}
6.   void m5() {throw new InterruptedException();}
7.   void m6() {throw new NoSuchMethodException();}
8. }

What is the result of attempting to compile the program?

a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above

Question 10


class A {
  public static void main (String[] args) {
    Object error = new Error();
    Object runtimeException = new RuntimeException();
    System.out.print((error instanceof Exception) + ",");
    System.out.print(runtimeException instanceof Exception);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Compiler Error
f. Run Time Error
g. None of the Above

Question 11


class Red {
  public static void main(String args[]) {
    for (int i = 0; i<5 ;i++) {
      switch(i) {
        case 0: System.out.print("v ");break;
        case 1: System.out.print("w ");
        case 2: System.out.print("x ");break;
        case 3: System.out.print("y ");
        case 4: System.out.print("z ");break;
        default: System.out.print("d ");
      }
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: v w x y z
b. Prints: v w x y z d
c. Prints: v w x x y z z
d. Prints: v w w x y y z d
e. Prints: d d d d d d
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 12


class A {
  public static void main (String[] args) {
    Error error = new Error();
    Exception exception = new Exception();
    System.out.print((exception instanceof Throwable)  + ",");
    System.out.print(error instanceof Throwable);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Compiler Error
f. Run Time Error
g. None of the Above

Question 13


class Orange {
  public static void main(String args[]) {
    byte b = -1;
    switch(b) {
      case 0:
        System.out.print("zero ");
      break;
      case 100:
        System.out.print("100 ");
      break;
      case 1000:
        System.out.print("1000 ");
      break;
      default: System.out.print("Default ");
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: zero
b. Prints: 100
c. Prints: 1000
d. Prints: Default
e. Runtime error
f. Compiler error
g. None of the above

Question 14


class Violet {
  public static void main(String args[]) {
    int x = -5;
    int success = 0;
    do  {
      switch(x) {
        case 0: System.out.print("0");
          x += 5; break;
        case 1: System.out.print("1");
          x += 3; break;
        case 2: System.out.print("2");
          x += 1; break;
        case 3: System.out.print("3");
          success++; break;
        case 4: System.out.print("4");
          x -= 1; break;
        case 5: System.out.print("5");
          x -= 4; break;
        case 6: System.out.print("6");
          x -= 5; break;
        default:
          x += x < 0 ? 2 : -2;
      }
    } while ((x != 3) || (success < 2));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0123456
b. Prints: 60514233
c. Prints: 1433
d. Prints: 61433
e. Prints: 051433
f. Prints: 33
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 15


class F {
  static int m1(String s, int i) {
    System.out.print(s + i);
    return i;
  }
  public static void main (String[] args) {
    int i = 0, j = 0, k = 0;
      do while (m1("i", ++i) < 2)
        System.out.print("k" + ++k);
      while (m1("j", ++j) < 2);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: i1k1i2k2j1i3j2
b. Prints: i1k1i2k2j1i1k1i2k2j2
c. Prints: i1k1i2j1i3j2
d. Runtime Exception
e. Compiler Error
f. None of the Above

Question 16


class I {
  public static void main (String[] args) {
    int i = 0, j = 9;
    do {
      if (j < 4) {
        break;
      } else if (j-- < 7) {
        continue;
      }
      i++;
    } while (i++ < 7);
    System.out.print(i + "," + j);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 4,7
b. Prints: 6,6
c. Prints: 6,5
d. Prints: 6,4
e. Prints: 7,5
f. Prints: 8,4
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 17


class A {
  public static void main (String[] args) {
    int j = 0;
      do for (int i = 0; i++ < 2;)
        System.out.print(i);
      while (j++ < 2);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Runtime Exception
j. Compiler Error
k. None of the Above

Question 18


class G {
  public static void main (String[] args) {
    int i = 0, j = 9;
    do {
      i++;
      if (j-- < i++) {
        break;
      }
    } while (i < 5);
    System.out.print(i + "," + j);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 5,4
b. Prints: 6,3
c. Prints: 6,6
d. Prints: 7,2
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 19


class C {
  public static void main (String[] args) {
    int j = 0;
    for (int i = 0; i++ < 2;) do
      System.out.print(i);
    while (j++ < 2);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Runtime Exception
j. Compiler Error
k. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1